home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libcc / address.cc next >
C/C++ Source or Header  |  1997-05-23  |  5KB  |  213 lines

  1.  
  2. #include "pi-source.h"
  3. #include "pi-address.h"
  4.  
  5. static inline int hi(const unsigned int x) { return (x >> 4) & 0x0f; }
  6. static inline int lo(const unsigned int x) { return x & 0x0f; }
  7. static inline int pair(const unsigned int x, const unsigned int y) 
  8. {
  9.      return (x << 4) | y;
  10. }
  11.  
  12. addressAppInfo_t::addressAppInfo_t(void *ai) 
  13.      : appInfo_t(ai) 
  14. {
  15.      unsigned char *ptr = ((unsigned char *) ai) + BASE_APP_INFO_SIZE;
  16.  
  17.      _dirtyFieldLabels = get_long(ptr);
  18.  
  19.      ptr += 4;
  20.      (void) memcpy(_labels, ptr, 352);
  21.  
  22.      ptr += 352;
  23.      _country = get_short(ptr);
  24.  
  25.      ptr += 2;
  26.      _sortByCompany = get_byte(ptr);
  27.  
  28.      int i;
  29.      for (i = 3; i < 8; i++)
  30.       (void) strcpy(_phoneLabels[i-3], _labels[i]);
  31.  
  32.      for (i = 19; i < 22; i++)
  33.       (void) strcpy(_phoneLabels[i-14], _labels[i]);
  34. }
  35.  
  36. void *addressAppInfo_t::pack(void) 
  37. {
  38.      unsigned char *buffer = new unsigned char [ADDRESS_APP_INFO_SIZE];
  39.      
  40.      baseAppInfoPack(buffer);
  41.      
  42.      unsigned char *ptr = buffer + BASE_APP_INFO_SIZE;
  43.      
  44.      set_long(ptr, _dirtyFieldLabels);
  45.  
  46.      ptr += 4;
  47.      memcpy(ptr, _labels, 352);
  48.  
  49.      ptr += 352;
  50.      set_short(ptr, _country);
  51.  
  52.      ptr += 2;
  53.      set_byte(ptr, _sortByCompany);
  54.  
  55.      return buffer;
  56. }
  57.  
  58. address_t::address_t(const address_t &oldCopy) 
  59. {
  60.      (void) memcpy(this, &oldCopy, sizeof(address_t));
  61.  
  62.      int len;
  63.      
  64.      for (short int i = 0; i < 19; i++)
  65.       if (oldCopy._entry[i]) {
  66.            len = strlen(oldCopy._entry[i]);
  67.            _entry[i] = new char [len + 1];
  68.            (void) strcpy(_entry[i], oldCopy._entry[i]);
  69.       }
  70. }
  71.  
  72. void address_t::unpack(void *buf, int firstTime) 
  73. {
  74.      int i;
  75.      
  76.      if (!firstTime)
  77.       for (i = 0; i < 19; i++)
  78.            if (_entry[i])
  79.             delete _entry[i];
  80.  
  81.      unsigned char *ptr = ((unsigned char *) buf) + 1;
  82.  
  83.      _whichPhone = hi(get_byte(ptr));
  84.      _phoneLabels[4] = lo(get_byte(ptr));
  85.      _phoneLabels[3] = hi(get_byte(++ptr));
  86.      _phoneLabels[2] = lo(get_byte(ptr));
  87.      _phoneLabels[1] = hi(get_byte(++ptr));
  88.      _phoneLabels[0] = lo(get_byte(ptr));
  89.  
  90.      unsigned long contents = get_long(++ptr);
  91.  
  92.      ptr += 5;
  93.  
  94.      int len;
  95.      for (i = 0; i < 19; i++) {
  96.       if (contents & (1 << i)) {
  97.            len = strlen((char *) ptr) + 1;
  98.            _entry[i] = new char [len];
  99.            (void) strcpy(_entry[i], (char *) ptr);
  100.            ptr += len;
  101.       } else
  102.            _entry[i] = NULL;
  103.      }
  104. }
  105.  
  106. address_t::~address_t(void) 
  107. {
  108.      for (int i = 0; i < 19; i++)
  109.       if (_entry[i])
  110.            delete _entry[i];
  111. }
  112.  
  113. void *address_t::internalPack(unsigned char *buf) 
  114. {
  115.      unsigned char offset = 0;
  116.      int len;
  117.      recordid_t contents = 0;
  118.  
  119.      unsigned char *ptr = buf + 9;
  120.      
  121.      for (short int i = 0; i < 19; i++) {
  122.       if (_entry[i]) {
  123.            len = strlen(_entry[i]) + 1;
  124.            contents |= (1 << i);
  125.            memcpy(ptr, _entry[i], len);
  126.            ptr += len;
  127.       } else
  128.            len = 0;
  129.       
  130.       if (i < address_t::company)
  131.            offset += len;
  132.      }
  133.  
  134.      if (_entry[address_t::company])
  135.       ++offset;
  136.      else
  137.       offset = 0;
  138.      
  139.      recordid_t phoneFlag = _phoneLabels[0];
  140.      phoneFlag |= _phoneLabels[1] << 4;
  141.      phoneFlag |= _phoneLabels[2] << 8;
  142.      phoneFlag |= _phoneLabels[3] << 12;
  143.      phoneFlag |= _phoneLabels[4] << 16;
  144.      phoneFlag |= _whichPhone << 20;
  145.  
  146.      set_long(ptr = buf, phoneFlag);
  147.  
  148.      ptr += 4;
  149.      set_long(ptr, contents);
  150.  
  151.      ptr += 4;
  152.      set_byte(ptr, offset);
  153.  
  154.      return buf;
  155. }
  156.  
  157. void *address_t::pack(int *len) 
  158. {
  159.      *len = 9;
  160.  
  161.      for (short int i = 0; i < 19; i++)
  162.       if (_entry[i] && _entry[i][0] != '\0')
  163.            *len += strlen(_entry[i]) + 1;
  164.      
  165.      unsigned char *ret = new unsigned char [*len];
  166.      return internalPack(ret);
  167. }
  168.  
  169. void *address_t::pack(void *buf, int *len) 
  170. {
  171.      int totalLength = 9;
  172.  
  173.      for (short int i = 0; i < 19; i++)
  174.       if (_entry[i] && _entry[i][0] != '\0')
  175.            totalLength += strlen(_entry[i]) + 1;
  176.  
  177.      if (*len < totalLength)
  178.           return NULL;
  179.  
  180.      *len = totalLength;
  181.  
  182.      return internalPack((unsigned char *) buf);
  183. }
  184.  
  185. // We can't just point to the data, as it might be deleted.  Make a copy
  186. void addressList_t::merge(address_t &address) 
  187. {
  188.      address._next = _head;
  189.      _head = new address_t(address);
  190. }
  191.  
  192. // We can't just point to the data in the list, as it might get deleted on
  193. // us. We need to make a real copy
  194. void addressList_t::merge(addressList_t &list) 
  195. {
  196.      address_t *newguy;
  197.  
  198.      for (address_t *ptr = list._head; ptr != NULL; ptr = ptr->_next) {
  199.           newguy = new address_t(ptr);
  200.           newguy->_next = _head;
  201.           _head = newguy;
  202.      }
  203. }
  204.  
  205. addressList_t::~addressList_t(void) {
  206.      address_t *ptr, *next;
  207.  
  208.      for (ptr = _head; ptr != NULL; ptr = next) {
  209.           next = ptr->_next;
  210.           delete ptr;
  211.      }
  212. }
  213.